/** 
 * Description:首页广告维护
 * Date:2013-1-31
 * Author:Along	
*/
package com.community.shopping.web;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.community.shopping.domain.GoodsType;
import com.community.shopping.domain.IndexItem;
import com.community.shopping.service.logic.GoodsTypeManager;
import com.community.shopping.service.logic.IndexItemManager;
import com.community.shopping.util.FileUpload;
import com.community.shopping.util.FileUtil;
import com.community.shopping.util.RequestHelp;

@Controller 
public class IndexItemControl {
	@Autowired  
    private IndexItemManager indexItemManager;
	@Autowired  
    private GoodsTypeManager goodsTypeManager;
	
	/**
	 * 列表广告管理
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itemlist")
	 public ModelAndView getitemlist(HttpServletRequest request) 
    { 
	 String  kind=request.getParameter("kind");
     ModelAndView modelAndView = new ModelAndView("index/item_"+kind+".jsp");     
     Integer shopId=RequestHelp.getShopId(request);
     List items=new ArrayList();
     List toptypes=goodsTypeManager.getTopGoodsType(shopId);
     for(int i=0;i<toptypes.size();i++)
     {
    	GoodsType type=(GoodsType)toptypes.get(i);
    	IndexItem item=indexItemManager.getIndexItemByType(type);   	
    	if(kind.equals("type")&&StringUtils.isNotBlank(item.getSubTypeIds()))
    	{
    		List goodstypes=goodsTypeManager.getGoodsType(" id in ("+item.getSubTypeIds()+")");
    		item.setSubTypes(goodstypes);
    	}
    	items.add(item);
     }    
	 modelAndView.addObject("items", items); 	 
     return modelAndView; 		
    } 
	/**
	 * 上传列表图片
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itemUpload")
	 public void itemUpload(HttpServletRequest request,HttpServletResponse response) throws Exception
	    { 
		  String sort=request.getParameter("sort");
		  String systemBase=request.getSession().getServletContext().getRealPath("/");
		  String folder=systemBase+"/data/"+RequestHelp.getShopId(request);
		  FileUtil.createPath(folder, "index,item");		   
	      String serverFileName=FileUpload.fileUpload(request,"Filedata",folder+"/index/item/");
	      String data="{\"url\":\"/data/"+RequestHelp.getShopId(request)+"/index/item/"+serverFileName+"\",\"sort\":\""+sort+"\"}";
	      //response.getWriter().print("/data/"+RequestHelp.getShopId(request)+"/index/item/"+serverFileName); 			  		
	      response.getWriter().print(data);
	    } 
	
	/**
	 * 列表广告保存
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itembansave")
	 public void itemadvsave(HttpServletRequest request,HttpServletResponse response) throws Exception   
    { 
     Integer shopId=RequestHelp.getShopId(request);
     String id=request.getParameter("id");
     IndexItem item=indexItemManager.getIndexItemById(Integer.parseInt(id));
     String imgName=request.getParameter("imgName");
     String imgUrl=request.getParameter("imgUrl");
     item.setBanerImg(imgName);
     item.setBanerUrl(imgUrl);
     item.setShopId(shopId);
     indexItemManager.save(item);    
    } 
	
	/**
	 * 列表分类子类别查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itemtypesquery")
	 public void itemtypesquery(HttpServletRequest request,HttpServletResponse response) throws Exception
    { 
	     String ids=request.getParameter("ids");
	     List<GoodsType> goodsTypes=goodsTypeManager.getGoodsType("id in ("+ids+")");
	     StringBuilder sb=new StringBuilder("[");
			for(GoodsType goodstype:goodsTypes)
			{
				if(sb.length()>1)
				{
					sb.append(",");	
				}
				sb.append("{");
				sb.append("\"id\":\""+goodstype.getId()+"\",");
				sb.append("\"typeName\":\""+goodstype.getTypeName()+"\"");
				sb.append("}");					
			}
			sb.append("]");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(sb.toString());			
    } 
		
	/**
	 * 列表分类保存
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itemtypesave")
	 public void itemtypesave(HttpServletRequest request,HttpServletResponse response) throws Exception
    { 
		 Integer shopId=RequestHelp.getShopId(request);
	     String id=request.getParameter("id");
	     GoodsType type=goodsTypeManager.getGoodsType(Integer.parseInt(id));
	     IndexItem item=indexItemManager.getIndexItemByType(type);
	     String typeids=request.getParameter("typeids");
	     item.setSubTypeIds(typeids);
	     item.setShopId(shopId);
	     indexItemManager.save(item);    	
    } 
	/**
	 * 头广告图片保存
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itemfirstsave")
	 public void itemfirstsave(HttpServletRequest request,HttpServletResponse response) throws Exception
    { 
		 Integer shopId=RequestHelp.getShopId(request);
	     String id=request.getParameter("id");
	     IndexItem item=indexItemManager.getIndexItemById(Integer.parseInt(id));
	     String imgName=request.getParameter("imgName");
	     String imgUrl=request.getParameter("imgUrl");
	     item.setFirstImg(imgName);
	     item.setFirstUrl(imgUrl);
	     item.setShopId(shopId);
	     indexItemManager.save(item);  
	 } 
	/**
	 * 尾广告图片保存
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/itemendsave")
	 public void itemendsave(HttpServletRequest request,HttpServletResponse response) throws Exception
    { 
		 Integer shopId=RequestHelp.getShopId(request);
	     String id=request.getParameter("id");
	     IndexItem item=indexItemManager.getIndexItemById(Integer.parseInt(id));
	     String imgName=request.getParameter("imgName");
	     String imgUrl1=request.getParameter("imgUrl1");
	     String imgUrl2=request.getParameter("imgUrl2");
	     String imgUrl3=request.getParameter("imgUrl3");
	     item.setEndImg(imgName);
	     item.setEndUrl1(imgUrl1);
	     item.setEndUrl2(imgUrl2);
	     item.setEndUrl3(imgUrl3);
	     item.setShopId(shopId);
	     indexItemManager.save(item);  
	 } 
	/**
	 * 保存商品
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/itemgoodssave")
	 public void itemgoodssave(HttpServletRequest request,GoodsType goodsType) throws Exception 
    { 
		 Integer shopId=RequestHelp.getShopId(request);
	     String id=request.getParameter("id");
	     IndexItem item=indexItemManager.getIndexItemById(Integer.parseInt(id));
	     String goodsids=request.getParameter("goodsids");
	     item.setGoodsIds(goodsids);
	     item.setShopId(shopId);
	     indexItemManager.save(item);
    } 
}
